package com.bistu.sim.xwy.news.Other;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bistu.sim.xwy.news.Adapter.GoodsAdapter;
import com.bistu.sim.xwy.news.R;
import com.bistu.sim.xwy.news.domain.Goods;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.bistu.sim.xwy.news.Http.JsonParse;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class SalerGoodsActivity extends AppCompatActivity {
    private  String the_id;
    private  String the_desc;
    private  String the_image;
    private String the_name;

    private Goods goods;
    private List<Goods> list;

    private GoodsAdapter adapter;

    private static final String TAG = "SalerGoodsActivity";

    private TextView saler_name;

    private  TextView saler_desc;

    private ImageView saler_image;

    private ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_saler_goods);
        initview();
        DoGet();
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                goods = list.get(position);
                Intent intent = new Intent(SalerGoodsActivity.this, GoodsPageActivity.class);
                Random random = new Random();
                int sold = random.nextInt(200);
                String goods_sold = String.valueOf(sold);
                intent.putExtra("sold",goods_sold);
                intent.putExtra("content",goods.getGoods_content());
                intent.putExtra("image",goods.getGoods_imageaddress());
                intent.putExtra("name",goods.getGoods_name());
                intent.putExtra("price",goods.getGoods_priece());
                intent.putExtra("type",goods.getGoods_type());
                startActivity(intent);
            }
        });
    }

    private void initview() {
        Intent intent = getIntent();
        if(intent != null){
            the_id = intent.getStringExtra("id");
            the_desc = intent.getStringExtra("desc");
            the_name = intent .getStringExtra("name");
            the_image = intent.getStringExtra("image");
        }
        saler_desc = (TextView) findViewById(R.id.saler_desc);
        saler_name = (TextView) findViewById(R.id.saler_name);
        saler_image = (ImageView) findViewById(R.id.saler_image);
        listView = (ListView)findViewById(R.id.goods_list);



        saler_name.setText(the_name);
        saler_desc.setText(the_desc);
        RequestOptions requestOptions = new RequestOptions()
                .placeholder(R.drawable.saler_load);
        Glide.with(getApplicationContext()).load(getString(R.string.saler_url)+the_image)
                .apply(requestOptions)
                .into(saler_image);

    }

    public void DoGet(){
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(8000, TimeUnit.MILLISECONDS)
                .build();
        Request request = new Request.Builder()
                .get()
                .url(getString(R.string.saler_url)+"/goods/AllGoods?bus_user_id="+the_id)
                .build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                Log.e(TAG,"请求失败"+e.toString());

            }
            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                String body=response.body().string();
                Log.e(TAG,"请求成功"+response.code());
                Log.e(TAG,"请求数据"+body);
                Log.e(TAG, "onResponse: "+ getString(R.string.saler_url)+"goods/AllGoods?bus_user_id="+the_id);
                Gson gson = new Gson();
                list = gson.fromJson(body, new TypeToken<List<Goods>>(){}.getType());
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        adapter = new GoodsAdapter(SalerGoodsActivity.this,list);
                        listView.setAdapter(adapter);
                    }
                });
            }
        });
    }
}